from scrapy import Request
import os

from scrapy.pipelines.images import ImagesPipeline


class DownImagePipeline(ImagesPipeline):
    def get_media_requests(self, item, info):
        for image_url in item['image_url']:
            # print(item['image_url'])
            yield Request(image_url, meta={'item': item})

    def media_downloaded(self, response, request, info):
        # print('media_downloaded开始下载:')
        (vpath, vname) = os.path.split(request.url)
        item = response.meta['item']

        filepath = os.path.join(item['image_floder'], vname)
        # print('media_downloaded:' + filepath)
        if not os.path.exists(filepath):
            print('media_downloaded开始下载:' + filepath)
            with open(filepath, "wb") as f:
                f.write(response.body)
            print('下载完成：'+filepath)
        else:
            print('已存在')
